﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using System.Media;

namespace DoAnHuongRung
{
    public partial class frmMonAn : Form
    {
        public frmMonAn()
        {
            InitializeComponent();
        }

        private void frmMonAn_Load(object sender, EventArgs e)
        {
            LoadDanhSach();
            LoadLoaiMon();
            LoadNVL();
        }

        private void LoadLoaiMon()
        {
            cbbLoaiMon.DisplayMember = "TenLoai";
            cbbLoaiMon.ValueMember = "MaLoai";
            cbbLoaiMon.DataSource = LoaiMonAnBUS.SelectAll();
        }
        private void LoadNVL()
        {
            cbbNguyenVatLieu.DisplayMember = "TenNVL";
            cbbNguyenVatLieu.ValueMember = "MaNVL";
            cbbNguyenVatLieu.DataSource = NguyenVatLieuBUS.SelectAll();
        }
        private void LoadDanhSach()
        {
            dgvDanhSach.AutoGenerateColumns = false;

            DataTable dt = MonAnBUS.SelectAll();
            foreach (DataRow dr in dt.Rows)
            {
                string Gia = dr["DonGia"].ToString();
                Gia = Gia.Split('.')[0];
                //Gia = String.Format("{0:0,0}", Convert.ToInt32(Gia));
                dr["DonGia"] = Convert.ToInt32(Gia);
            }
            dgvDanhSach.DataSource = dt;

            txtMaMon.Text = "";
            txtTenMon.Text = "";
            txtDonGia.Text = "";

            txtDonViTinh.Text = "";
            nudSoLuong.Value = 0;

            foreach (DataGridViewRow dr in dgvNguyenVatLieu.Rows)
            {
                dgvNguyenVatLieu.Rows.Remove(dr);
            }
        }
        private void LoadDSNVL(string MaMon)
        {
            dgvNguyenVatLieu.DataSource = ChiTietMonAnBUS.SelectByMaMonAn(Convert.ToInt32(MaMon));
            nudSoLuong.Value = 0;
            txtDonViTinh.Text = "";
        }
        private void SelectData(string filterExpression)
        {
            DataTable pData = MonAnBUS.SelectAll();
            DataRow[] rows = pData.Select(filterExpression);

            pData = ((DataTable)dgvDanhSach.DataSource).Clone();
            for (int i = 0; i < rows.Length; i++)
            {
                DataRow row = pData.NewRow();
                row[0] = rows[i].ItemArray[0].ToString();
                row[1] = rows[i].ItemArray[1].ToString();
                row[2] = rows[i].ItemArray[2].ToString();
                row[3] = rows[i].ItemArray[3].ToString();
                row[4] = rows[i].ItemArray[4].ToString();
                pData.Rows.Add(row);
            }
            dgvDanhSach.DataSource = pData;
        }

        private void txtTim_Enter(object sender, EventArgs e)
        {
            txtTim.Text = "";
            txtTim.ForeColor = Color.Black;
        }
        private void txtTim_Leave(object sender, EventArgs e)
        {
            if (txtTim.Text == "")
            {
                txtTim.Text = "Tìm kiếm";
                txtTim.ForeColor = Color.Silver;
            }
        }
        private void txtTim_TextChanged(object sender, EventArgs e)
        {
            if (txtTim.Text != "") SelectData("TenMonAn like '%" + txtTim.Text + "%'");
            else dgvDanhSach.DataSource = MonAnBUS.SelectAll();
        }

        private void dgvDanhSach_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDanhSach.CurrentCell != null)
            {
                txtMaMon.Text = dgvDanhSach.CurrentRow.Cells[0].Value.ToString();
                txtTenMon.Text = dgvDanhSach.CurrentRow.Cells[1].Value.ToString();
                cbbLoaiMon.Text = dgvDanhSach.CurrentRow.Cells[3].Value.ToString();
                txtDonGia.Text = dgvDanhSach.CurrentRow.Cells[4].Value.ToString();

                if (txtMaMon.Text != "")
                    LoadDSNVL(txtMaMon.Text);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaMon.Text != "")
            {
                txtMaMon.Text = "";
                txtTenMon.Text = "";
                txtDonGia.Text = "";
                txtTenMon.Focus();
            }
            else if (txtTenMon.Text == "")
            {
                MessageBox.Show("Nhập tên món ăn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txtTenMon.Focus();
            }
            else if (txtDonGia.Text == "")
            {
                MessageBox.Show("Nhập đơn giá", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txtDonGia.Focus();
            }
            else
            {
                //Thêm
                bool b = MonAnBUS.Insert(new MonAnDTO(0, txtTenMon.Text, (int)cbbLoaiMon.SelectedValue, txtDonGia.Text));
                if (b == true)
                {
                    LoadDanhSach();
                    new SoundPlayer(Properties.Resources.Windows_Notify).Play();
                    MessageBox.Show("Đã thêm !!!");
                }
            }
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaMon.Text == "")
                MessageBox.Show("Chọn món ăn cần sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else if (txtTenMon.Text == "")
            {
                MessageBox.Show("Nhập tên món ăn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txtTenMon.Focus();
            }
            else if (txtDonGia.Text == "")
            {
                MessageBox.Show("Nhập đơn giá", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txtDonGia.Focus();
            }
            else
            {
                //Sửa
                bool b = MonAnBUS.Update(new MonAnDTO(Convert.ToInt32(txtMaMon.Text), txtTenMon.Text, (int)cbbLoaiMon.SelectedValue, txtDonGia.Text));
                if (b == true)
                {
                    LoadDanhSach();
                    new SoundPlayer(Properties.Resources.Windows_Notify).Play();
                    MessageBox.Show("Đã sửa !!!");
                }
            }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaMon.Text == "")
                MessageBox.Show("Chọn món ăn cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else
            {
                try
                {
                    //Xóa
                    bool b = ChiTietMonAnBUS.DeleteByMaMon(Convert.ToInt32(txtMaMon.Text));
                    bool c = MonAnBUS.Delete(Convert.ToInt32(txtMaMon.Text));
                    if (b && c)
                    {
                        LoadDanhSach();
                        new SoundPlayer(Properties.Resources.Windows_Notify).Play();
                        MessageBox.Show("Đã xóa !!!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi: " + ex.Message);
                }
            }
        }

        private void btnThemNVL_Click(object sender, EventArgs e)
        {
            if (txtMaMon.Text == "")
                MessageBox.Show("Chọn món ăn");
            else if (nudSoLuong.Value == 0)
                MessageBox.Show("Đổi số lượng");
            else if (txtDonViTinh.Text == "")
                MessageBox.Show("Nhập đơn vị tính");
            else
            {
                if (ChiTietMonAnBUS.Insert(new ChiTietMonAnDTO(0,
                    Convert.ToInt32(txtMaMon.Text),
                    (int)cbbNguyenVatLieu.SelectedValue,
                    txtDonViTinh.Text,
                    (int)nudSoLuong.Value)))
                    LoadDSNVL(txtMaMon.Text);
            }
        }
        private void btnXoaNVL_Click(object sender, EventArgs e)
        {
            if (txtMaMon.Text == "")
                MessageBox.Show("Chọn món ăn");
            else
            {
                foreach (DataGridViewRow dr in dgvNguyenVatLieu.SelectedRows)
                {
                    ChiTietMonAnBUS.Delete((int)dr.Cells[0].Value);
                    dgvNguyenVatLieu.Rows.Remove(dr);
                }
            }
        }
    }
}
